package com.martin.dms.web.beans;

import java.io.IOException;
import java.io.Serializable;

import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.web.multipart.MultipartFile;

import com.martin.dms.model.Category;
import com.martin.dms.model.Item;

public class ItemBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3425052311298262701L;

    private Long id;
    private String name;

    private Long categoryId;
    private String price;
    private String description;

    private transient MultipartFile image;

    private byte[] imageBytes;

    public ItemBean() {

    }

    public ItemBean(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public ItemBean(Item item) {
        this.id = item.getId();
        this.name = item.getName();
        Category category = item.getCategory();
        if (category != null) {
            this.categoryId = category.getId();
        }
        this.price = String.valueOf(item.getPrice());
        this.description = item.getDescription();
        this.imageBytes = item.getImage();
    }

    /**
     * @param id the id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the id.
     */
    public Long getId() {
        return id;
    }

    /**
     * @param name the name to set.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the name.
     */
    public String getName() {
        return name;
    }

    /**
     * @param categoryId the categoryId to set.
     */
    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    /**
     * @return the categoryId.
     */
    public Long getCategoryId() {
        return categoryId;
    }

    /**
     * @param price the price to set.
     */
    public void setPrice(String price) {
        this.price = price;
    }

    /**
     * @return price.
     */
    public String getPrice() {
        return price;
    }

    /**
     * @return the integer price.
     */
    public Integer getIntPrice() {
        return Integer.valueOf(price);
    }

    /**
     * @param description the description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the description.
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param image the image to set
     * @throws IOException
     */
    public void setImage(MultipartFile image) throws IOException {
        this.image = image;
        if (!image.isEmpty()) {
            this.imageBytes = image.getBytes();
        }
    }

    /**
     * @return the image
     */
    public MultipartFile getImage() {
        return image;
    }

    /**
     * @param imageBytes the imageBytes to set
     */
    public void setImageBytes(byte[] imageBytes) {
        this.imageBytes = imageBytes;
    }

    /**
     * @return the imageBytes
     */
    public byte[] getImageBytes() {
        return imageBytes;
    }

    public void validateItemsForm(ValidationContext validationContext) {
        MessageContext messageContext = validationContext.getMessageContext();
        String userEvent = validationContext.getUserEvent();
        if (userEvent.equals("save")) {
            if (name == null || "".equals(name)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.name").build());
            } else if (price == null || "".equals(price)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.price").build());
            } else {
                try {
                    Integer.valueOf(price);
                } catch (NumberFormatException nbe) {
                    messageContext.addMessage(new MessageBuilder().error().code("error.integerfield.price").build());
                }
            }
        }
    }
    
    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (!(o instanceof ItemBean)) {
            return false;
        }
        ItemBean bean = (ItemBean) o;
        if (bean.id == null || this.id == null) {
            return false;
        }
        return (bean.id.equals(this.id));
    }
    
    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}
